﻿$(document).ready(function () {

    var map;
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var pontoPadrao;
    
    function initialize(lat, lng, end) {
       
        directionsDisplay = new google.maps.DirectionsRenderer();
       
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {

                pontoPadrao = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                map.setCenter(pontoPadrao);

                var geocoder = new google.maps.Geocoder();
             
                geocoder.geocode({
                    "location": new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
                },
                function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        $("#txtEnderecoPartida").val(results[0].formatted_address);
                    }
                });
            });
        }
        
        var myOptions = {
            zoom: 15,
            center: pontoPadrao,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        directionsDisplay.setMap(map);
        
        if (pontoPadrao !== null && end === null) {
            navigator.geolocation.getCurrentPosition(function (position) {

                pontoPadrao = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                var marker = new google.maps.Marker({
                    position: pontoPadrao,
                    map: map,
                    title: "Você está aqui!"
                });
            });
        }
         
        if (end !== null) {
            var request = {
                origin: $("#txtEnderecoPartida").val(),
                destination: end,
                travelMode: google.maps.TravelMode.DRIVING
            };

            directionsService.route(request, function (result, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    $("#trajeto-texto").html("");
                    directionsDisplay.setDirections(result);
                    directionsDisplay.setPanel(document.getElementById("trajeto-texto"));                    
                }
            });
        }        
    }

    initialize(null, null, null);
 
    $('#btnShowLocations').click(function () {
        $("#map_canvas").html("Carregando...");
        var url = "/ConsultaMedicos/GetLocations";
        var idMedico = $(this).parent().find("#item_IDMedico").val();
        //console.log(idMedico);
        $.getJSON(url, { idMedico: idMedico }, function (data) {
            $.each(data, function (index, LocationData) {

                initialize(LocationData.Latitude, LocationData.Longitude, LocationData.Endereco);
                               
            });
        });
    });
});